perf: skip snapshot rebuild in mergeDuplicates when names are unique - #2441
Merged
zeitlinger merged 2 commits intoSep 15, 2026
Merged
Conversation
david-mollitor-db
requested review from
dhoard,
fstab,
jaydeluca and
zeitlinger
as code owners
September 1, 2026 19:07
MetricSnapshots is always sorted by prometheus name, so duplicate names are adjacent. Detect duplicates in a single allocation-free pass and, when there are none (the common case), return the input unchanged instead of rebuilding it through a LinkedHashMap, an ArrayList per group, a MetricSnapshots.Builder and a freshly sorted MetricSnapshots. The merge path for actual duplicates is unchanged. Output is byte-identical (verified by the existing exposition-format tests, including DuplicateNamesExpositionTest). Signed-off-by: David Mollitor <david.mollitor@databricks.com>
david-mollitor-db
force-pushed
the
merge-duplicates-fast-path
branch
from
September 14, 2026 14:04
e286dcf to
b7221bc
Compare
Contributor
Benchmark resultsBenchmark run succeeded for
Prometheus Java Client BenchmarksRun Information
Comparison with base
Results for PR headCounterBenchmark
HistogramBenchmark
HistogramTextFormatBenchmark
TextFormatUtilBenchmark
Raw ResultsNotes
Benchmark Descriptions
|
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
zeitlinger
approved these changes
Sep 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
TextFormatUtil.mergeDuplicatescurrently only short-circuits when there is a single snapshot. For every scrape with two or more metric families it unconditionally builds aLinkedHashMap, anArrayListper group, aMetricSnapshots.Builder(with its own list + set) and a freshly sortedMetricSnapshots— even when there is nothing to merge, which is the common case.Since
MetricSnapshotsis always sorted by prometheus name (see its constructor), any duplicate names are adjacent. This adds a single allocation-free pass to detect duplicates; when there are none, the input is returned unchanged.The merge path for actual duplicates is unchanged.
Why
It removes the map / per-group list / builder / re-sort allocations on every scrape that has no duplicate metric names. Measured on a JMH benchmark of a histogram-heavy scrape (collect + serialize to the Prometheus text format), allocation on the serialize path dropped by ~880 B/op, scaling with the number of metric families.
Correctness
Output is byte-identical. The existing exposition-format tests pass, including
DuplicateNamesExpositionTest, which exercises the merge path with real duplicate names.